import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import i18n from '../lang'

import qs from 'qs'

const service = axios.create({
  // baseURL: process.env.BASE_API,
  timeout: 30000
})

// http request 拦截器
service.interceptors.request.use(
  config => {
    config.withCredentials = true
    config.headers = {
      'Content-Type': 'application/json; charset=UTF-8'
    }
    if (config.method === 'get') {
      config.paramsSerializer = function (params) {
        return qs.stringify(params, {arrayFormat: 'repeat'})
      }
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
// http response 拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code.substr(-4) !== '0000') {
      if (i18n.t(`errorMsg.${res.code}`) !== `errorMsg.${res.code}`) {
        Message({
          dangerouslyUseHTMLString: true,
          showClose: true,
          message: i18n.t(`errorMsg.${res.code}`),
          type: 'error'
        })
      } else {
        if (res.code === 'ESF-PROCESS_CONTAMINATION-0001') {
          return Promise.resolve(res)
        } else {
          Message({
            dangerouslyUseHTMLString: true,
            showClose: true,
            message: res.msg,
            type: 'error'
          })
        }
      }
      return Promise.reject(res)
    } else {
      if (res.msg) {
        Message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: 'success'
        })
      }
    }
    return res
  },
  error => {
    let originalRequest = error.config
    if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1 && !originalRequest._retry) {
      Message({
        dangerouslyUseHTMLString: true,
        showClose: true,
        message: error.message,
        type: 'error'
      })
    } else {
      const res = error.response
      if (res === undefined) {
        Message({
          dangerouslyUseHTMLString: true,
          showClose: true,
          message: error.message,
          type: 'error'
        })
      } else {
        if (res.status === 401) {
          Message({
            dangerouslyUseHTMLString: true,
            showClose: true,
            message: res.status + '：' + res.data.message,
            type: 'error'
          })
          if (res.data.error === 'Unauthorized') {
            store.dispatch('LogOut').then(() => {
              window.location.href = locale.platform + '?code=' + store.getters.code
            })
          }
        } else if (res.status === 403) {
          Message({
            dangerouslyUseHTMLString: true,
            showClose: true,
            message: res.status + '：' + res.data.msg,
            type: 'error'
          })
        } else if (res.status === 400) {
          Message({
            dangerouslyUseHTMLString: true,
            showClose: true,
            message: res.status + '：' + res.data.error_description,
            type: 'error'
          })
        } else if (res.status === 202) {
          // this.$router.push({path: '/'})
        } else if (res.status === 503) {
          Message({
            dangerouslyUseHTMLString: true,
            showClose: true,
            message: res.status + '：' + res.data,
            type: 'error'
          })
        } else {
          Message({
            dangerouslyUseHTMLString: true,
            showClose: true,
            message: res.status + '：' + res.data.message,
            type: 'error'
          })
        }
      }
    }
    return Promise.reject(error)
  }
)

export default service
